package com.itheima.Controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.Pojo.ShoppingCart;
import com.itheima.Service.ShoppingCartService;
import com.itheima.common.BaseContext;
import com.itheima.common.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    @GetMapping("/list")
    public R<List<ShoppingCart>> checkShoppingCart(){
        LambdaQueryWrapper<ShoppingCart>lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        lambdaQueryWrapper.orderByAsc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(lambdaQueryWrapper);
        return R.success(list);

    }

    /**
     * 添加商品至购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        //设置用户的id，指定当前是那个用户的购物车数据
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);

        //查询当前菜品或者套餐是否在购物车中
        Long dishId = shoppingCart.getDishId();

        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ShoppingCart::getUserId,currentId);
        if(dishId!=null){
            //将菜品添加至购物车
            lambdaQueryWrapper.eq(ShoppingCart::getDishId,dishId);
        }else{
            //将套餐添加至购物车
            lambdaQueryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }

       //获取用户唯一对应的数组
        ShoppingCart one = shoppingCartService.getOne(lambdaQueryWrapper);

        if(one!=null){
            //不等于空，说明已有套餐，那么要再加1

            log.info(String.valueOf(shoppingCart.getNumber()));
            Integer number = one.getNumber();
            //如果已存在，在原来的数量上+1
            one.setNumber(number+1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.updateById(one);
        }else{
            //等于空，将页面传过来的数据存入购物车
            shoppingCart.setNumber(1);
            shoppingCartService.save(shoppingCart);
            one=shoppingCart;
        }
        return R.success(one);
    }

    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> clean(){
        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        shoppingCartService.remove(lambdaQueryWrapper);
        return R.success("清空成功");
    }

    /**
     * 购物车已添加列表减少
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart){
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);

        LambdaQueryWrapper<ShoppingCart>lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ShoppingCart::getUserId,shoppingCart.getUserId());
        ShoppingCart subOne = shoppingCartService.getOne(lambdaQueryWrapper);
        Integer number = subOne.getNumber();
        if(number!=0){
            subOne.setNumber(number-1);
            shoppingCartService.updateById(subOne);
            subOne=shoppingCart;
        }
        LambdaQueryWrapper<ShoppingCart> lambdaQueryWrapper1=new LambdaQueryWrapper<>();
        lambdaQueryWrapper1.eq(ShoppingCart::getUserId,shoppingCart.getUserId());
        shoppingCartService.remove(lambdaQueryWrapper1);


        return R.success(subOne);
    }
}
